package game

import (
	"qm_system_server/server/global"
	"qm_system_server/server/model/game"
    gameReq "qm_system_server/server/model/game/request"
)

type QmConfigService struct {
}

// CreateQmConfig 创建qmConfig表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmConfigService *QmConfigService) CreateQmConfig(qmConfig *game.QmConfig) (err error) {
	err = global.GVA_DB.Create(qmConfig).Error
	return err
}

// DeleteQmConfig 删除qmConfig表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmConfigService *QmConfigService)DeleteQmConfig(ID string) (err error) {
	err = global.GVA_DB.Delete(&game.QmConfig{},"id = ?",ID).Error
	return err
}

// DeleteQmConfigByIds 批量删除qmConfig表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmConfigService *QmConfigService)DeleteQmConfigByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]game.QmConfig{},"id in ?",IDs).Error
	return err
}

// UpdateQmConfig 更新qmConfig表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmConfigService *QmConfigService)UpdateQmConfig(qmConfig game.QmConfig) (err error) {
	err = global.GVA_DB.Save(&qmConfig).Error
	return err
}

// GetQmConfig 根据ID获取qmConfig表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmConfigService *QmConfigService)GetQmConfig(ID string) (qmConfig game.QmConfig, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&qmConfig).Error
	return
}

// GetQmConfigInfoList 分页获取qmConfig表记录
// Author [piexlmax](https://github.com/piexlmax)
func (qmConfigService *QmConfigService)GetQmConfigInfoList(info gameReq.QmConfigSearch) (list []game.QmConfig, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&game.QmConfig{})
    var qmConfigs []game.QmConfig
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Name != "" {
        db = db.Where("name LIKE ?","%"+ info.Name+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&qmConfigs).Error
	return  qmConfigs, total, err
}
